/**
 * Created by lixiaodi on 2018/9/27 0027.
 */

mini.parse();
var grid = mini.get("datagrid1");
//grid.load(ajaxUrl + "/role/list.do");
//grid.sortBy("createtime", "desc");                  //排序
var url = bootPATH + "../frame/frame1/pages/";      //路径前缀
var menu = new ColumnsMenu(grid);
//初始化
$(function(){
	httpRequest();
});
//增加
function add() {
    mini.open({
        url: url +"alertBox/roleWindow.html",
        title: "新增角色", width: 800, height: 650,
        onload: function () {
            var iframe = this.getIFrameEl();
        },
        ondestroy: function (action) {
        	httpRequest();
        }
    });
}
//编辑
function edit() {
    var row = grid.getSelected();
    console.log(row);
    if (row) {
        mini.open({
            url: url +"alertBox/roleWindow.html",
            title: "编辑角色", width: 800, height: 650,
            onload: function () {
                var iframe = this.getIFrameEl();
                var data = { action: "edit", id: row.roleId };
                iframe.contentWindow.SetData(data);
            },
            ondestroy: function (action) {
            	httpRequest();
            }
        });
    } else {
        mini.alert("请选中一条记录");
    }
}
//删除
function remove() {
    var rows = grid.getSelecteds();
    if (rows.length > 0) {
        mini.confirm("确定删除选中记录？","系统提示",function(action){
            if (action == "ok"){
                var ids = [];
                for (var i = 0, l = rows.length; i < l; i++) {
                    var r = rows[i];
                    ids.push(r.roleId);
                }
                var id = ids.join(',');
                grid.loading("操作中，请稍后......");
                $.ajax({
                    url: ajaxUrl + "/role/deleteRole.do?roleId=" + id,
                    type:"post",
                    success: function (data) {
                    	if(data.state == 'error'){
                    		grid.unmask();
                    		mini.alert(data.message);
                    	}else{
                    		httpRequest();
                    	}
                    },
                    error: function () {
                        mini.alert('失败')
                    }
                });
            }
        })
    } else {
        mini.alert("请选中一条记录");
    }
}
//查询
function search() {
    var key = mini.get("key").getValue();
    $.ajax({
        url: ajaxUrl + "/DepartmentController/getListByParam.do",
        type:"post",
        datatype:"json",
        data:{
            departmentName:key
        },
        success: function (data) {
            grid.setData(data.data);
            //grid.reload();
        },
        error: function () {
            mini.alert("失败");
        }
    });
}
function onKeyEnter(e) {
    search();
}
//刷新
function refresh(){
	httpRequest();
}
//数据请求
function httpRequest(){

	$.ajax({
        url: ajaxUrl + "/role/list.do",
        type:"post",
        success: function (data) {
            console.log("963");
        	var roleData = data.data;
        	//日期格式化
        	for(var i = 0; i < roleData.length; i++){
        		//创建日期
        		if(roleData[i].createDate){
        			roleData[i].createDate = formatDate(roleData[i].createDate);
        		}else{
        			roleData[i].createDate = '-';
        		}
        	}
        	grid.setData(roleData);
        },
        error:function(data){
            mini.alert(data.message);
        }
    });
}
function onRowselect(e) {

    statuss = true;
    console.log(e,'勾选/取消勾选时');
    var senderEl=e.sender;
    var recordobj=e.record;
    var senderElData=senderEl.getData();
    console.log(senderElData);
    // for(var i=0;i<senderElData.length;i++){
    //     if(senderElData[i].no==recordobj.no){
    //         senderEl.select(senderElData[i],true);
    //     }
    // }

}